#TidyTuesday

Import libraries and data

## -- Attaching packages --------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1     v purrr   0.3.2
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   0.8.3     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts ------------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## loaded package tidyverse
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
## loaded package plotly
## here() starts at C:/Users/evan/Documents/TidyTuesday
## loaded package here
## Parsed with column specification:
## cols(
##   city = col_character(),
##   state = col_character(),
##   city_size = col_character(),
##   mode = col_character(),
##   n = col_double(),
##   percent = col_double(),
##   moe = col_double(),
##   state_abb = col_character(),
##   state_region = col_character()
## )
## file found. loading from local
#summary state by mode
locations <- locations %>% group_by(state) %>% mutate(state.tot=sum(n))

#summary state by mode
locations <- locations %>% group_by(state, mode) %>% mutate(state.mode.tot=sum(n))

#summary state by city size and mode
locations <- locations %>% group_by(state, city_size, mode) %>% mutate(state.cat.tot=sum(n))

#wrangle for cloropleth
#cloro <- locations %>% group_by(state_abb, city_size, mode) %>% summarise(n=sum(n), num.cities=n(), prop=mean(percent)) %>% mutate(per.city=n/num.cities)

locations_reduced <- locations %>% select(city, city_size, state, mode, n, percent, moe, state_abb, lon, lat, state.mode.tot, state.cat.tot, state.tot)


#set geo
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = "#C0C0C0", bgcolor = "#38383a", domain=list(x=c(-1,2), y=c(-1,2)), framewidth=0
)

#init map
map <- plot_geo(locationmode = 'USA-states') %>%

  #add individual traces for each subset (mode x city size)  
  add_trace(
    data = locations_reduced %>% group_by(state_abb, city_size, mode) %>% summarise(n=sum(n), num.cities=n(), prop=mean(percent)) %>% mutate(per.city=n/num.cities) %>% filter(city_size == "Small", mode == "Bike"),
    z = ~n,
    hoverinfo="none",
    locations = ~ state_abb,
    colorscale = c("Greens"),
    reversescale = T,
    visible = T,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Bikers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = locations_reduced %>% group_by(state_abb, city_size, mode) %>% summarise(n=sum(n), num.cities=n(), prop=mean(percent)) %>% mutate(per.city=n/num.cities) %>% filter(city_size == "Medium", mode == "Bike"),
    z = ~n, 
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Bikers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = locations_reduced %>% group_by(state_abb, city_size, mode) %>% summarise(n=sum(n), num.cities=n(), prop=mean(percent)) %>% mutate(per.city=n/num.cities) %>% filter(city_size == "Large", mode == "Bike"),
    z = ~ n,
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Bikers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = locations_reduced %>% group_by(state_abb, city_size, mode) %>% summarise(n=sum(n), num.cities=n(), prop=mean(percent)) %>% mutate(per.city=n/num.cities) %>% filter(city_size == "Small", mode == "Walk"),
    z = ~ n,
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Walkers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = "
    "
  ) %>%
  add_trace(
    data = locations_reduced %>% group_by(state_abb, city_size, mode) %>% summarise(n=sum(n), num.cities=n(), prop=mean(percent)) %>% mutate(per.city=n/num.cities) %>% filter(city_size == "Medium", mode == "Walk"),
    z = ~ n,
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Walkers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = locations_reduced %>% group_by(state_abb, city_size, mode) %>% summarise(n=sum(n), num.cities=n(), prop=mean(percent)) %>% mutate(per.city=n/num.cities) %>% filter(city_size == "Large", mode == "Walk"),
    z = ~ n,
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Walkers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_markers(data = locations_reduced %>% filter(city_size == "Small", mode == "Bike"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
              text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " bikers / ", state.mode.tot, " ", state_abb, " bikers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " bikers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " small city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=T
  ) %>%
  add_markers(data = locations_reduced %>% filter(city_size == "Medium", mode == "Bike"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
               text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " bikers / ", state.mode.tot, " ", state_abb, " bikers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " bikers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " medium city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>%
  add_markers(data = locations_reduced %>% filter(city_size == "Large", mode == "Bike"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
               text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " bikers / ", state.mode.tot, " ", state_abb, " bikers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " bikers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " large city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"),  
              showlegend=F, visible=F
  ) %>%
  add_markers(data = locations_reduced %>% filter(city_size == "Small", mode == "Walk"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
               text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " walkers / ", state.mode.tot, " ", state_abb, " walkers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " walkers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " small city walkers / ", state.mode.tot, " ", state_abb, " walkers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " walkers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>% 
  add_markers(data = locations_reduced %>% filter(city_size == "Medium", mode == "Walk"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
              text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " walkers / ", state.mode.tot, " ", state_abb, " walkers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " walkers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " medium city walkers / ", state.mode.tot, " ", state_abb, " walkers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " walkers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>%
  add_markers(data = locations_reduced  %>% filter(city_size == "Large", mode == "Walk"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
              text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " walkers / ", state.mode.tot, " ", state_abb, " walkers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " walkers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " large city walkers / ", state.mode.tot, " ", state_abb, " walkers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " walkers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>%
  
  layout(title = list(text = '<br><br>Commuters by State',
                      font=list(color = "#008888", size = 20)),
         geo = g, 
         autosize=T,
         margin = list(l=0,r=0, b=0, t=0, pad=0.01),
         paper_bgcolor = "#38383a",
         updatemenus = list(
           list(
           type = 'buttons',
           direction = "right",
           xanchor="right",
           yanchor="bottom",
           x=0.499, y=0.075,
           bgcolor="lightgreen",
           font= list(
             size=9
           ),
           showactive=F,
           buttons = list(
             list(
               method = "update",
               args = list(list(
                 visible = c(T, F, F, F, F, F,T, F, F, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Small Cities - Bikers"
              ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, T, F, F, F, F,F, T, F, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Medium Cities - Bikers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, T, F, F, F,F, F, T, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Large Cities - Bikers"
             ))),
           list(
           type = 'buttons',
           direction = "right",
           xanchor="left",
           yanchor="bottom",
           x=.501, y=.075,
           bgcolor = "lightblue",
           font= list(
             size=9
           ),
           showactive=F,
           buttons = list(
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, T, F, F,F, F, F, T, F, F),
                 colorscale = c("Blues")
               )),
               label = "Small Cities - Walkers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, F, T, F,F, F, F, F, T, F),
                 colorscale = c("Blues")
               )),
               label = "Medium Cities - Walkers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, F, F, T,F, F, F, F, F, T),
                 colorscale = c("Blues")
               )),
               label = "Large Cities - Walkers"
             )
           )
         ))) %>%
  add_annotations(text = "Hover for breakdown - Use bottons to change city size and Commuter Mode",
                     xref = "paper", yref = "paper",
                     x = .05, xanchor = "left",
                     y = .05, yanchor = "top",
                     legendtitle = TRUE,
                     showarrow = FALSE,
                     font = list(color = '#008888',
                              size = 11))
## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

Representation of daily commuters by location surveyed. Buttons the map toggle by city size (small, medium or large) and commute mode (bikers or walkers). Each state is colored by total number of commuters of the selected mode and each yellow marker indicates individual cities surveye. Highlight individual cities to get more detailed information about

#Spliting to meet plotly api_create() upload limits

Cloropleth

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`
cloro <- plot_geo(locationmode = 'USA-states') %>%

  #add individual traces for each subset (mode x city size)  
  add_trace(
    data =cholo_data %>% filter(city_size == "Small", mode == "Bike"),
    z = ~n,
    text = ~paste0("</br>", state, "<br />",
                            state.cat.tot, " ", state_abb, " small city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
    locations = ~ state_abb,
    colorscale = c("Greens"),
    reversescale = T,
    visible = T,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Bikers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = cholo_data %>% filter(city_size == "Medium", mode == "Bike"),
    z = ~n,
    text = ~paste0("</br>", state, "<br />",
                            state.cat.tot, " ", state_abb, " small city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Bikers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = cholo_data %>% filter(city_size == "Large", mode == "Bike"),
    z = ~ n,
    text = ~paste0("</br>", state, "<br />",
                            state.cat.tot, " ", state_abb, " small city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Bikers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = cholo_data %>% filter(city_size == "Small", mode == "Walk"),
    z = ~ n,
    text = ~paste0("</br>", state, "<br />",
                            state.cat.tot, " ", state_abb, " small city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Walkers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = "
    "
  ) %>%
  add_trace(
    data = cholo_data %>% filter(city_size == "Medium", mode == "Walk"),
    z = ~ n,
    text = ~paste0("</br>", state, "<br />",
                            state.cat.tot, " ", state_abb, " small city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Walkers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = cholo_data %>% filter(city_size == "Large", mode == "Walk"),
    z = ~ n,
    text = ~paste0("</br>", state, "<br />",
                            state.cat.tot, " ", state_abb, " small city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Walkers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  layout(title = list(text = '<br><br>Commuters by State',
                      font=list(color = "#008888", size = 20)),
         geo = g, 
         autosize=T,
         margin = list(l=0,r=0, b=0, t=0, pad=0.01),
         paper_bgcolor = "#38383a",
         updatemenus = list(
           list(
           type = 'buttons',
           direction = "right",
           xanchor="right",
           yanchor="bottom",
           x=0.499, y=0.075,
           bgcolor="lightgreen",
           font= list(
             size=9
           ),
           showactive=F,
           buttons = list(
             list(
               method = "update",
               args = list(list(
                 visible = c(T, F, F, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Small Cities - Bikers"
              ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, T, F, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Medium Cities - Bikers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, T, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Large Cities - Bikers"
             ))),
           list(
           type = 'buttons',
           direction = "right",
           xanchor="left",
           yanchor="bottom",
           x=.501, y=.075,
           bgcolor = "lightblue",
           font= list(
             size=9
           ),
           showactive=F,
           buttons = list(
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, T, F, F),
                 colorscale = c("Blues")
               )),
               label = "Small Cities - Walkers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, F, T, F),
                 colorscale = c("Blues")
               )),
               label = "Medium Cities - Walkers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, F, F, T),
                 colorscale = c("Blues")
               )),
               label = "Large Cities - Walkers"
             )
           )
         ))) %>%
  add_annotations(text = "Hover for breakdown - Use bottons to change city size and Commuter Mode",
                     xref = "paper", yref = "paper",
                     x = .05, xanchor = "left",
                     y = .05, yanchor = "top",
                     legendtitle = TRUE,
                     showarrow = FALSE,
                     font = list(color = '#008888',
                              size = 11))
cloro
## Found a grid already named: 'Daily commuter Cloropleth Grid'. Since fileopt='overwrite', I'll try to update it
## Found a plot already named: 'Daily commuter Cloropleth'. Since fileopt='overwrite', I'll try to update it
## file found. loading from local
#summary state by mode
locations <- locations %>% group_by(state) %>% mutate(state.tot=sum(n))

#summary state by mode
locations <- locations %>% group_by(state, mode) %>% mutate(state.mode.tot=sum(n))

#summary state by city size and mode
locations <- locations %>% group_by(state, city_size, mode) %>% mutate(state.cat.tot=sum(n))

#wrangle for cloropleth
#cloro <- locations %>% group_by(state_abb, city_size, mode) %>% summarise(n=sum(n), num.cities=n(), prop=mean(percent)) %>% mutate(per.city=n/num.cities)

locations_reduced <- locations %>% select(city, city_size, state, mode, n, percent, moe, state_abb, lon, lat, state.mode.tot, state.cat.tot, state.tot)


#set geo
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = "#C0C0C0", bgcolor = "#38383a", domain=list(x=c(-1,2), y=c(-1,2)), framewidth=0
)

#init map
points <- plot_mapbox(locationmode = 'USA-states') %>%

  #add individual traces for each subset (mode x city size)  
  add_markers(data = locations_reduced %>% filter(city_size == "Small", mode == "Bike"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              hoverinfo = "text",
              text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " bikers / ", state.mode.tot, " ", state_abb, " bikers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " bikers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " small city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=T
  ) %>%
  add_markers(data = locations_reduced %>% filter(city_size == "Medium", mode == "Bike"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n, 
              hoverinfo = "text",
               text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " bikers / ", state.mode.tot, " ", state_abb, " bikers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " bikers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " medium city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>%
  add_markers(data = locations_reduced %>% filter(city_size == "Large", mode == "Bike"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n, 
              hoverinfo = "text",
               text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " bikers / ", state.mode.tot, " ", state_abb, " bikers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " bikers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " large city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"),  
              showlegend=F, visible=F
  ) %>%
  add_markers(data = locations_reduced %>% filter(city_size == "Small", mode == "Walk"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n, 
              hoverinfo = "text",
               text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " walkers / ", state.mode.tot, " ", state_abb, " walkers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " walkers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " small city walkers / ", state.mode.tot, " ", state_abb, " walkers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " walkers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>% 
  add_markers(data = locations_reduced %>% filter(city_size == "Medium", mode == "Walk"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n, 
              hoverinfo = "text",
              text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " walkers / ", state.mode.tot, " ", state_abb, " walkers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " walkers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " medium city walkers / ", state.mode.tot, " ", state_abb, " walkers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " walkers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>%
  add_markers(data = locations_reduced  %>% filter(city_size == "Large", mode == "Walk"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n, 
              hoverinfo = "text",
              text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " walkers / ", state.mode.tot, " ", state_abb, " walkers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " walkers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " large city walkers / ", state.mode.tot, " ", state_abb, " walkers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " walkers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>%
  
  layout(title = list(text = '<br><br>Commuters by State',
                      font=list(color = "#008888", size = 20)),
          mapbox=list(
                center=list(lon=~median(lon), lat=~median(lat)), 
                zoom =5, 
                bearing=0,
                pitch=15,
                style='basic'),
         geo = g, 
         autosize=T,
         margin = list(l=0,r=0, b=0, t=0, pad=0.01),
         paper_bgcolor = "#38383a",
         updatemenus = list(
           list(
           type = 'buttons',
           direction = "right",
           xanchor="right",
           yanchor="bottom",
           x=0.499, y=0.075,
           bgcolor="lightgreen",
           font= list(
             size=9
           ),
           showactive=F,
           buttons = list(
             list(
               method = "update",
               args = list(list(
                 visible = c(T, F, F, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Small Cities - Bikers"
              ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, T, F, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Medium Cities - Bikers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, T, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Large Cities - Bikers"
             ))),
           list(
           type = 'buttons',
           direction = "right",
           xanchor="left",
           yanchor="bottom",
           x=.501, y=.075,
           bgcolor = "lightblue",
           font= list(
             size=9
           ),
           showactive=F,
           buttons = list(
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, T, F, F),
                 colorscale = c("Blues")
               )),
               label = "Small Cities - Walkers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, F, T, F),
                 colorscale = c("Blues")
               )),
               label = "Medium Cities - Walkers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, F, F, T),
                 colorscale = c("Blues")
               )),
               label = "Large Cities - Walkers"
             )
           )
         ))) %>%
  add_annotations(text = "Hover for breakdown - Use bottons to change city size and Commuter Mode",
                     xref = "paper", yref = "paper",
                     x = .05, xanchor = "left",
                     y = .05, yanchor = "top",
                     legendtitle = TRUE,
                     showarrow = FALSE,
                     font = list(color = '#008888',
                              size = 11))
points
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.
## Warning: 'scattermapbox' objects don't have these attributes: 'locationmode'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'lon', 'lat', 'mode', 'text', 'hovertext', 'line', 'connectgaps', 'marker', 'fill', 'fillcolor', 'textfont', 'textposition', 'selected', 'unselected', 'hoverinfo', 'hovertemplate', 'subplot', 'idssrc', 'customdatasrc', 'lonsrc', 'latsrc', 'textsrc', 'hovertextsrc', 'hoverinfosrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scattermapbox' objects don't have these attributes: 'locationmode'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'lon', 'lat', 'mode', 'text', 'hovertext', 'line', 'connectgaps', 'marker', 'fill', 'fillcolor', 'textfont', 'textposition', 'selected', 'unselected', 'hoverinfo', 'hovertemplate', 'subplot', 'idssrc', 'customdatasrc', 'lonsrc', 'latsrc', 'textsrc', 'hovertextsrc', 'hoverinfosrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scattermapbox' objects don't have these attributes: 'locationmode'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'lon', 'lat', 'mode', 'text', 'hovertext', 'line', 'connectgaps', 'marker', 'fill', 'fillcolor', 'textfont', 'textposition', 'selected', 'unselected', 'hoverinfo', 'hovertemplate', 'subplot', 'idssrc', 'customdatasrc', 'lonsrc', 'latsrc', 'textsrc', 'hovertextsrc', 'hoverinfosrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scattermapbox' objects don't have these attributes: 'locationmode'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'lon', 'lat', 'mode', 'text', 'hovertext', 'line', 'connectgaps', 'marker', 'fill', 'fillcolor', 'textfont', 'textposition', 'selected', 'unselected', 'hoverinfo', 'hovertemplate', 'subplot', 'idssrc', 'customdatasrc', 'lonsrc', 'latsrc', 'textsrc', 'hovertextsrc', 'hoverinfosrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scattermapbox' objects don't have these attributes: 'locationmode'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'lon', 'lat', 'mode', 'text', 'hovertext', 'line', 'connectgaps', 'marker', 'fill', 'fillcolor', 'textfont', 'textposition', 'selected', 'unselected', 'hoverinfo', 'hovertemplate', 'subplot', 'idssrc', 'customdatasrc', 'lonsrc', 'latsrc', 'textsrc', 'hovertextsrc', 'hoverinfosrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scattermapbox' objects don't have these attributes: 'locationmode'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'lon', 'lat', 'mode', 'text', 'hovertext', 'line', 'connectgaps', 'marker', 'fill', 'fillcolor', 'textfont', 'textposition', 'selected', 'unselected', 'hoverinfo', 'hovertemplate', 'subplot', 'idssrc', 'customdatasrc', 'lonsrc', 'latsrc', 'textsrc', 'hovertextsrc', 'hoverinfosrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Finished!

###########################3 #leftovers

Evan W Barba

2019-11-05